﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using CodeGame.ServerObjects;

namespace CodeGame.Networking.Serverside.ClientServerPackets
{
    internal class ScriptedPacket : ServerPacket
    {
        private static Dictionary<int, string> HookLinks = new Dictionary<int, string>();

        public static void AddHook(int id, HookObject obj)
        {
            ScriptedPacket.HookLinks.Add(id, "NETHOOK_" + id.ToString());
            ServerSingletons.Hooks.Add("NETHOOK_" + id.ToString(), obj);
        }

        public override ClientServerNet GetID()
        {
            return ClientServerNet.ScriptedPacket;
        }

        public override void Handle(Lidgren.Network.NetIncomingMessage message, Lidgren.Network.NetConnection sender)
        {
            int id = message.ReadInt32();

            if (ScriptedPacket.HookLinks.ContainsKey(id))
            {
                ServerSingletons.Hooks.Call(ScriptedPacket.HookLinks[id], message.ReadString(), null, null);
            }
        }
    }
}
